//
//  CZEnrollViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZEnrollViewController.h"
#import "CZPayViewController.h"

@interface CZEnrollViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *enrollScrollView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *discountLabel;
@property (weak, nonatomic) IBOutlet UITextField *inviteCodeTF;
@property (nonatomic, copy) NSString *productId;
@property (nonatomic, copy) NSString *inviteId;


@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *enrollBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation CZEnrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"线上报名";
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = YES;
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
    self.inviteCodeTF.delegate = self;
    [self getEnrollData];
    [self setBtnIsEnable];
    // Do any additional setup after loading the view from its nib.
}

- (void)setBtnIsEnable {
    self.vipEndDate = [self.vipEndDate substringToIndex:10];
    self.vipEndDate = [[self.vipEndDate stringByReplacingOccurrencesOfString:@"-" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSUInteger vipDateValue = self.vipEndDate.integerValue;
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *dateStr =  [formatter stringFromDate:date];
    NSUInteger currentDateValue = dateStr.integerValue;
    
    if (vipDateValue > currentDateValue) {
        self.enrollBtn.enabled = NO;
        self.inviteCodeTF.enabled = NO;
        self.enrollBtn.backgroundColor = [UIColor grayColor];
    } else {
        self.enrollBtn.enabled = YES;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField.text.length == 8) {
        [self getDiscountData];
    }
    
}

- (void)getDiscountData {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/invite-code/detail",APP_HOST];
    [SVProgressHUD showWithStatus:@"获取优惠信息"];
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"invite_code" : self.inviteCodeTF.text} success:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"] isEqualToString:@"200"]) {
            
            self.discountLabel.text = [NSString stringWithFormat:@"¥%@", response[@"result"][@"discount"]];
            self.inviteId = response[@"result"][@"invite_id"];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        
    }];
}

- (void)getEnrollData {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/product/member-passport",APP_HOST];
    [SVProgressHUD show];
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token]} success:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        if ([response[@"code"] isEqualToString:@"200"]) {
            self.titleLabel.text = response[@"result"][@"name"];
            self.priceLabel.text = [NSString stringWithFormat:@"¥%@", response[@"result"][@"price"]];
            self.contentLabel.text = response[@"result"][@"content"];
            self.productId = response[@"result"][@"product_id"];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];

    }];
}

//- (void)getOrderData {
//    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/order/create",APP_HOST];
//    NSDictionary *parma;
//    
////    if (self.inviteId) {
////        parma = @{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"product_id" : self.productId, @"invite_id" : self.inviteId};
////    } else {
//        parma = @{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"product_id" : self.productId};
////    }
//    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"product_id" : self.productId} success:^(NSDictionary *response) {
//        [SVProgressHUD dismiss];
//        if ([response[@"code"] isEqualToString:@"200"]) {
//            
//        } else {
//            self.enrollBtn.enabled = NO;
//            self.enrollBtn.backgroundColor = [UIColor grayColor];
//            [SVProgressHUD showErrorWithStatus:response[@"message"]];
//        }
//    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
//        [SVProgressHUD showErrorWithStatus:@"网络错误"];
//    }];
//}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.alpha = 1;

    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
}
- (IBAction)enrollAction:(UIButton *)sender {
    CZPayViewController *payVC = [[CZPayViewController alloc] init];
    payVC.productId = self.productId;
    if (self.inviteCodeTF.text.length == 8) {
//        payVC.discountCode = self.inviteCodeTF.text;
        if (self.inviteId.length > 0) {
            payVC.inviteId = self.inviteId;
        }
    }
//    NSString *discoutPrice = [self.discountLabel.text substringFromIndex:1];
//    NSString *price = [self.priceLabel.text substringFromIndex:1];

//    DLog(@"%@", discoutPrice);
//    if (discoutPrice.floatValue > 0) {
//        payVC.price = [NSString stringWithFormat:@"%.2f", price.floatValue - discoutPrice.floatValue];
//    } else {
//        payVC.price = [NSString stringWithFormat:@"%.2f", price.floatValue];
//    }
    [self.navigationController pushViewController:payVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
